#include "lcdv1.h"
#include <stc8h.h>
#include "adc1.h"
#include <STC8G_H_UART.h>
#include <STC8G_H_Delay.h>

bit B1S_Flag;
unsigned char code numcode[]={
    0xEB,       /*'0', 0*/
    0x60,       /*'1', 1*/
    0xC7,       /*'2', 2*/
    0xE5,       /*'3', 3*/
    0x6C,       /*'4', 4*/
    0xAD,       /*'5', 5*/
    0xAF,       /*'6', 6*/
    0xE0,       /*'7', 7*/
    0xEF,       /*'8', 8*/
    0xED,       /*'9', 9*/
    0xEE,       /*'A', 10*/
    0x2F,       /*'B', 11*/
    0x8B,       /*'C', 12*/
    0x67,       /*'D', 13*/
    0x8F,       /*'E', 14*/
    0x8E,       /*'F', 15*/
    0x04,       /*'-', 16*/
    0x00,       /*' ', 17*/
    0x10,       /*'.', 18*/
};
unsigned char code numcode1[]={
    0x7d,       /*'0', 0*/
    0x60,       /*'1', 1*/
    0x3E,       /*'2', 2*/
    0x7A,       /*'3', 3*/
    0x63,       /*'4', 4*/
    0x5B,       /*'5', 5*/
    0x5F,       /*'6', 6*/
    0x70,       /*'7', 7*/
    0x7F,       /*'8', 8*/
    0x7B,       /*'9', 9*/
    0x77,       /*'A', 10*/
    // 0x7E,       /*'0', 0*/
    0x4F,       /*'B', 11*/
    0x1D,       /*'C', 12*/
    0x6E,       /*'D', 13*/
    0x1F,       /*'E', 14*/
    0x17,       /*'F', 15*/
    0x02,       /*'-', 16*/
    0x00,       /*' ', 17*/
    0x80,       /*'.', 18*/

};
void RTC_init(void)
{

    X32KCR = 0x80; // 开启外部 32K晶振
    // IRC32KCR =0x80; //开启内部32k
    P1IE &= ~0xc0; // 关闭数字输入
    INIYEAR = 25;  // Y:2021
    INIMONTH = 4;  // M:12
    INIDAY = 5;    // D:31
    INIHOUR = 23;  // H:23
    INIMIN = 59;   // M:59
    INISEC = 50;   // S:50
    INISSEC = 0;   // S/128:0
    RTCCFG = 0x01; // 触发RTC寄存器初始化

    RTCIF = 0; // 清中断标志
    // RTCIEN = 0x08;                              //使能RTC秒中断
    RTCIEN = 0x08;
    RTCCR = 1;
}

void lcdinit()
{
    P_SW2 |= 0x80;
    X32KCR = 0x80; // 开启外部 32K晶振
    // IRC32KCR =0x80; //开启内部32k
    P1IE &= ~0xc0; // 关闭数字输入
    P0M0 &= ~0x7f;
    P0M1 |= 0x7f;
    P1M0 = 0x00;
    P1M1 = 0xc0;
    P2M0 &= ~0xe7;
    P2M1 |= 0xe7;
    P3M0 &= ~0x60;
    P3M1 |= 0x60;
    P4M0 &= ~0x7e;
    P4M1 |= 0x7e;
    P5M0 &= ~0x0f;
    P5M1 |= 0x0f;
    P6M0 = 0x00;
    P6M1 = 0x00;
    P7M0 &= ~0x0f;
    P7M1 |= 0x0f;

    P0IE &= ~0x7f;
    P1IE &= ~0xc0;
    P2IE &= ~0xe7;
    P3IE &= ~0x60;
    P4IE &= ~0x7e;
    P5IE &= ~0x0f;
    P7IE &= ~0x0f;

    LCDCFG = 0x80;
    DBLEN = 2;
    COMLENH = 0x00;
    COMLENM = 0x00;
    COMLENL = 0x65;
    BLINKRATE = 60;
    COMON = 0x0f;  // COM使能寄存器
    SEGON1 = 0x00; // SEG线使能寄存器1, SEG7~SEG0
    SEGON2 = 0xff; // SEG线使能寄存器2, SEG15~SEG8
    SEGON3 = 0x3f; // SEG线使能寄存器3, SEG23~SEG16
    SEGON4 = 0x7f; // SEG线使能寄存器4, SEG31~SEG24
    SEGON5 = 0x0f; // SEG线使能寄存器5, SEG39~SEG32

    LCDCR = 0x01;

    C0SEGV3 = 0xff;
    C1SEGV3 = 0xff;
    C2SEGV3 = 0xff;
    C3SEGV3 = 0xff;
    C0SEGV4 = 0xff;
    C1SEGV4 = 0xff;
    C2SEGV4 = 0xff;
    C3SEGV4 = 0xff;
    C0SEGV2 = 0xff;
    C1SEGV2 = 0xff;
    C2SEGV2 = 0xff;
    C3SEGV2 = 0xff;
    C0SEGV1 = 0xff;
    C1SEGV1 = 0xff;
    C2SEGV1 = 0xff;
    C3SEGV1 = 0xff;
    delay_ms(200);
    delay_ms(200);
    delay_ms(200);
    delay_ms(200);
    delay_ms(200);
    C0SEGV1&=0x3f;
    C1SEGV1&=0x3f;
    C2SEGV1&=0x3f;
    C3SEGV1&=0x7f;
    
    C0SEGV4&=0xf2;
    C1SEGV4&=0xf2;
    C2SEGV4&=0xf0;
    C3SEGV4&=0xf2;



    
}

void writeLcd(u8 seg1, u8 seg2, u8 dat)
{       
    unsigned char lcdcom_ ;
    unsigned char lcdb_ ;
    unsigned char xdata * lcdMemBase;
    lcdMemBase = (unsigned char  xdata *) 0xfb90;


    lcdcom_=seg1 / 8;
    lcdb_=seg1 % 8;
    *(lcdMemBase+lcdcom_)&=~(0x01<<lcdb_);
    *(lcdMemBase+lcdcom_)|=(dat&0x01)<<lcdb_;
    dat>>=1;

    *(lcdMemBase+lcdcom_+8)&= ~(0x01<<lcdb_);
    *(lcdMemBase+lcdcom_+8)|= (dat&0x01)<<lcdb_;
    dat>>=1;

    *(lcdMemBase+lcdcom_+16)&= ~(0x01<<lcdb_);
    *(lcdMemBase+lcdcom_+16)|= (dat&0x01)<<lcdb_;
    dat>>=1;

    *(lcdMemBase+lcdcom_+24)&= ~(0x01<<lcdb_);
    *(lcdMemBase+lcdcom_+24)|= (dat&0x01)<<lcdb_;
    dat>>=1;
    
    lcdcom_=seg2/8;
    lcdb_=seg2%8;
    *(lcdMemBase+lcdcom_)&= ~(0x01<<lcdb_);
    *(lcdMemBase+lcdcom_)|= (dat&0x01)<<lcdb_;
    dat>>=1;

    *(lcdMemBase+lcdcom_+8)&= ~(0x01<<lcdb_);
    *(lcdMemBase+lcdcom_+8)|= (dat&0x01)<<lcdb_;
    dat>>=1;

    *(lcdMemBase+lcdcom_+16)&= ~(0x01<<lcdb_);
    *(lcdMemBase+lcdcom_+16)|= (dat&0x01)<<lcdb_;
    dat>>=1;

    *(lcdMemBase+lcdcom_+24)&= ~(0x01<<lcdb_);
    *(lcdMemBase+lcdcom_+24)|= (dat&0x01)<<lcdb_;
    dat>>=1;

}
void shownum1(u8 s1,u8 s2 ,u8 s3 ,u8 s4){
    writeLcd(24,25,numcode[s1]);
    writeLcd(26,27,numcode[s2]);
    writeLcd(28,29,numcode[s3]);
    writeLcd(30,32,numcode[s4]);
}
void shownum2(u8 s1,u8 s2 ,u8 s3 ,u8 s4,u8 s5,u8 s6){
    writeLcd(8,9,numcode1[s1]);
    writeLcd(10,11,numcode1[s2]);
    writeLcd(12,13,numcode1[s3]);
    writeLcd(16,17,numcode1[s4]);
    writeLcd(18,19,numcode1[s5]);
    writeLcd(20,21,numcode1[s6]);

}
void setvlcd(unsigned int vbat){
    char v;
    if(vbat<=4800 && vbat >=3300){
        v=(int)(((3300/(vbat/100))-65)/5);
        LCDCFG=0x80+v;}
    if(vbat>4800){
        LCDCFG=0x80;
    }
    if(vbat<3300){
        LCDCFG=0x87;
    }
    
}
void showv(u16 vbat){
    if(vbat>4000){
        C0SEGV4|=0x04;
        C1SEGV4|=0x04;
        C2SEGV4|=0x04;
        C3SEGV4|=0x04;
    }
    if(vbat<=4000 && vbat >3800){
        C0SEGV4|=0x04;
        C1SEGV4|=0x04;
        C2SEGV4|=0x04;
        C3SEGV4&=0xfb;
    }
    if(vbat<=3800 && vbat >3600){
        C0SEGV4|=0x04;
        C1SEGV4|=0x04;
        C2SEGV4&=0xfb;
        C3SEGV4&=0xfb;
    }
    if(vbat<=3600 && vbat >3400){
        C0SEGV4|=0x04;
        C1SEGV4&=0xfb;
        C2SEGV4&=0xfb;
        C3SEGV4&=0xfb;
    }
    if(vbat<=3400){
        C0SEGV4&=0xfb;
        C1SEGV4&=0xfb;
        C2SEGV4&=0xfb;
        C3SEGV4&=0xfb;
    }
}
void showvbat(u16 vbat){
    u8 m1;
    u8 m2;
    u8 m3;
    u8 m4;
    m4=vbat%10;
    vbat /=10;
    m3=vbat%10;
    vbat /=10;
    m2=vbat%10;
    vbat /=10;
    m1=vbat%10;
    shownum1(m1,m2,m3,m4);
}
void showtime(void){
    shownum2(HOUR/10,HOUR%10,MIN/10,MIN%10,SEC/10,SEC%10);
}